/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;


using InterfaceFiles;
using Adiance.VMukti.BAL;
using NetPeerService;

namespace Adiance.VMukti.VCService
{
    public partial class VCServer : ServiceBase
    {
        NetPeerServer serverCommon = null;
        NetPeerServer serverLoginStat = null;
        NetPeerServer serverPageNav = null;
        NetPeerServer serverPodResize = null;
        NetPeerServer serverUriInfo = null;
        NetPeerServer serverLoginInfo = null;


        ServiceHost fileupload;
        //ServiceHost logininfo;


        ZipInfo.CliZipInfoMain c1 = null;
        CliLoginStatus c2 = null;
        CliUriInfo objUri = null;
        CliLoginInfo cliLogin = null;

        NetTcpBinding binding = new NetTcpBinding();

        public string meetingid = "";
        EventLog entLog = null;

        public VCServer()
        {
            InitializeComponent();
            binding.PortSharingEnabled = true;
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                entLog = new EventLog("Application", Environment.MachineName, "Adiance");

                UserInfo.Initialize();

                fileupload = new ServiceHost(typeof(CsvcFileTransfer));
                fileupload.AddServiceEndpoint(typeof(IsvcFileTransfer), binding, "net.tcp://" + UserInfo.strMachIP + ":2600/FileUpload");
                fileupload.Open();

                serverLoginInfo = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/LoginInfo");
                serverLoginInfo.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/LoginInfo");
                serverLoginInfo.OpenServer();
                cliLogin = new CliLoginInfo("net.tcp://" + UserInfo.strMachIP + ":2500/LoginInfo");

                c1 = new ZipInfo.CliZipInfoMain();

                serverUriInfo = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/UriInfo");
                serverUriInfo.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/UriInfo");
                serverUriInfo.OpenServer();
                objUri = new CliUriInfo("net.tcp://" + UserInfo.strMachIP + ":2500/UriInfo");


                serverLoginStat = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/LoginStatus");
                serverLoginStat.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/LoginStatus");
                serverLoginStat.OpenServer();
                c2 = new CliLoginStatus("net.tcp://" + UserInfo.strMachIP + ":2500/LoginStatus");

                serverPageNav = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/PageNavigation");
                serverPageNav.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/PageNavigation");
                serverPageNav.OpenServer();

                serverPodResize = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/PodResize");
                serverPodResize.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/PodResize");
                serverPodResize.OpenServer();                

                entLog.WriteEntry("All WCF Services are successfully hosted");
                entLog.Close();
            }
            catch (Exception exp)
            {             
                exp.Data.Add("My Key", "Adiance.VMukti.VCService--:--VCServer.cs--:--OnStart()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;

                entLog = new EventLog("Application", Environment.MachineName, "Adiance");
                entLog.WriteEntry("Catch of OnStart" + exp.Message);
                entLog.Close();
            }
        }

        protected override void OnStop()
        {
            try
            {
                entLog = new EventLog("Application", Environment.MachineName, "Adiance");

                #region NetTcp Services
                try
                {
                    if (fileupload.State != CommunicationState.Closed)
                    {
                        fileupload.Close();
                    }
                }
                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()1--:--");
                    Adiance.VMukti.BAL.clsUtility.LogException = exp;
                }

               
                #endregion


                #region NetPeer Services
                try
                {
                    serverLoginStat.CloseServer();
                    serverPageNav.CloseServer();
                    serverPodResize.CloseServer();
                    serverUriInfo.CloseServer();
                    serverLoginInfo.CloseServer();
                }
                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--VCServer.cs--:--OnStop()3--:--");
                    Adiance.VMukti.BAL.clsUtility.LogException = exp;
                }
                #endregion


                #region Hosted Services
                try
                {
                    for (int j = 0; j < CliLoginInfo.serverControlslist.Count; j++)
                    {
                        try
                        {
                            ((NetPeerServer)CliLoginInfo.serverControlslist[j][0]).CloseServer();
                        }
                        catch (Exception exp)
                        {
                            exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()4--:--");
                            Adiance.VMukti.BAL.clsUtility.LogException = exp;
                        } 
                    }
                }
                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()5--:--");
                    Adiance.VMukti.BAL.clsUtility.LogException = exp;
                }
                #endregion

                entLog.WriteEntry("All WCF Services closed successfully");
                entLog.Close();

            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCService--:--VCServer.cs--:--OnStop()1--:----:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
            }
        }
    }
}
